﻿using System;
using System.Collections.Generic;
using System.Data.Objects;
using System.Linq;

namespace Project5188.BUS.Operations
{
    public class InsuranceInfos : DetailView<insuranceinfo>
    {
        private project5188Entities _context;
        // ReSharper disable InconsistentNaming
        private new List<insuranceinfo> EntityList;
        private new insuranceinfo Entity;
        // ReSharper restore InconsistentNaming

        public long TransactionId { get; private set; }

        #region Overrides of DetailView<insuranceinfo>

        public override List<insuranceinfo> GetEntityList()
        {
            _context = new project5188Entities();
            EntityList = _context.insuranceinfoes.Where(a => !a.IsDeleted).OrderBy(a => a.a_ID).ToList();

            return EntityList;
        }

        public override List<insuranceinfo> GetEntityById(long id)
        {
            _context = new project5188Entities();
            EntityList = _context.insuranceinfoes.Where(a => a.a_ID == id && !a.IsDeleted).OrderBy(a => a.a_ID).ToList();

            return EntityList;
        }

        public override insuranceinfo GetEntity()
        {
            _context = new project5188Entities();
            Entity = new insuranceinfo();
            return Entity;
        }

        public override void Update()
        {
            _context.SaveChanges();
        }

        public override void Insert()
        {
            if (ExistPersonnel(Entity.b_PersonnelID))
            {
                throw new Exception("Seçtiğiniz personel için sistemde kayıt mevcuttur.");
            }

            _context.AddToinsuranceinfoes(Entity);
            _context.SaveChanges();
            TransactionId = Entity.a_ID;
        }

        #endregion

        public bool ExistPersonnel(long personnelId)
        {
            _context = new project5188Entities();

            return _context.communicationinfoes.Where(a => a.b_PersonnelID == personnelId && !a.IsDeleted).Count() > 0;
        }

        public List<insuranceinfo> GetEntityListByParameters(string registryNoText, string nameSurname, string workPlaceName, string securityCredentialsDocumentNoText, long availabilityWeaponId, string insuranceInfoPolicyNoText, string insuranceInfoCompanyName, DateTime? insuranceInfoStartDate)
        {
            _context = new project5188Entities();
            var parameterList = new List<ObjectParameter>();

            var dynamicString = "it.IsDeleted = @IsDeleted";
            parameterList.Add(new ObjectParameter("IsDeleted", false));

            dynamicString += " And it.personnelinfo.IsDeleted = @PersonnelIsDeleted";
            parameterList.Add(new ObjectParameter("PersonnelIsDeleted", false));

            if (!string.IsNullOrEmpty(registryNoText) && !String.IsNullOrWhiteSpace(registryNoText))
            {
                dynamicString += " And it.personnelinfo.b_RegisterNo = @RegisterNo";
                parameterList.Add(new ObjectParameter("RegisterNo", Convert.ToInt32(registryNoText)));
            }

            if (!string.IsNullOrEmpty(nameSurname) && !string.IsNullOrWhiteSpace(nameSurname))
            {
                dynamicString += " And it.personnelinfo.f_NameSurname = @PersonnelNameSurname";
                parameterList.Add(new ObjectParameter("PersonnelNameSurname", nameSurname));
            }

            if (!string.IsNullOrEmpty(workPlaceName) && !string.IsNullOrWhiteSpace(workPlaceName))
            {
                dynamicString += " And it.d_ReferenceWorkplace = @ReferenceWorkplace";
                parameterList.Add(new ObjectParameter("ReferenceWorkplace", workPlaceName));
            }

            if (!String.IsNullOrEmpty(securityCredentialsDocumentNoText) && !String.IsNullOrWhiteSpace(securityCredentialsDocumentNoText))
            {
                dynamicString += " And it.i_SecurityCredentialsDocumentNo = @SecurityCredentialsDocumentNo";
                parameterList.Add(new ObjectParameter("SecurityCredentialsDocumentNo", Convert.ToInt32(securityCredentialsDocumentNoText)));
            }

            if (availabilityWeaponId > 0)
            {
                dynamicString += " And it.m_AvailabilityWeaponsParameterValueID_6 = @AvailabilityWeaponId";
                parameterList.Add(new ObjectParameter("AvailabilityWeaponId", availabilityWeaponId));
            }

            if (!String.IsNullOrEmpty(insuranceInfoPolicyNoText) && !String.IsNullOrWhiteSpace(insuranceInfoPolicyNoText))
            {
                dynamicString += " And it.p_InsuranceInfoPolicyNo = @InsuranceInfoPolicyNo";
                parameterList.Add(new ObjectParameter("InsuranceInfoPolicyNo", Convert.ToInt32(insuranceInfoPolicyNoText)));
            }

            if (!String.IsNullOrEmpty(insuranceInfoCompanyName) && !String.IsNullOrWhiteSpace(insuranceInfoCompanyName))
            {
                dynamicString += " And it.r_InsuranceInfoCompanyName = @InsuranceInfoCompanyName";
                parameterList.Add(new ObjectParameter("InsuranceInfoCompanyName", insuranceInfoCompanyName));
            }

            if (insuranceInfoStartDate != null)
            {
                dynamicString += " And it.s_InsuranceInfoStartDate = @InsuranceInfoStartDate";
                parameterList.Add(new ObjectParameter("InsuranceInfoStartDate", insuranceInfoStartDate));
            }

            EntityList = _context.insuranceinfoes.Where(dynamicString, parameterList.ToArray()).OrderBy(a => a.a_ID).ToList();

            return EntityList;
        }
    }
}
